* Reset settings and initialize log file
launch, path("share/heterogeneity")

*-------------------------------------------------------------------------------
* Price and Wasserman (2024), "The Summer Drop in Female Employment"
*
* Description: Show demographic heterogeneity in the summer drop.
*-------------------------------------------------------------------------------


local m = 1
if "$estimate" != "0" {
	* Load data on adult individuals
	gzuse "$basepath/data/derived/cps_bms_sample.dta.gz", clear

	* Restrict to variables we need
	keep pid tm year month wtfinl tmspline* weeks female age_bin wbho educ emp

	* Standardize nomenclature
	rename age_bin age

	* Store the data
	tempfile core
	save `core'

	* Loop over sex
	foreach f of numlist 0 1 {
		* Loop over characteristics of interest
		foreach v in "age" "wbho" "educ" {
			* Reload the sample
			use if female == `f' using `core', clear
			gcollapse (mean) emp (rawsum) wtfinl (first) month tmspline* weeks [pw = wtfinl], by(`v' tm)
			quietly tsset `v' tm

			* Loop over values of each characteristic
			quietly levelsof `v', local(groups)
			foreach k of local groups {
				* Estimate specification
				quietly ivreg2 emp ib5.month tmspline* weeks if `v' == `k', bw($bandwidth) robust small
				process_estimates, path("heterogeneity") model("f`f'_`v'`k'") mayjuly
			}
		}
	}
}


* Prepare estimates
*-------------------------------------------------------------------------------

* Load estimates into memory
load_estimates, path("heterogeneity")


* Plot estimates
*-------------------------------------------------------------------------------

#delimit ;
coefplot
	(f1_age1, drop(*) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f0_age1, drop(*) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))

	(f1_age1, keep(mayjuly) rename(mayjuly = age1) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f1_age2, keep(mayjuly) rename(mayjuly = age2) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f1_age3, keep(mayjuly) rename(mayjuly = age3) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f1_age4, keep(mayjuly) rename(mayjuly = age4) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f1_age5, keep(mayjuly) rename(mayjuly = age5) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))

	(f1_wbho1, keep(mayjuly) rename(mayjuly = wbho1) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f1_wbho2, keep(mayjuly) rename(mayjuly = wbho2) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f1_wbho3, keep(mayjuly) rename(mayjuly = wbho3) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f1_wbho4, keep(mayjuly) rename(mayjuly = wbho4) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))

	(f1_educ1, keep(mayjuly) rename(mayjuly = educ1) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f1_educ2, keep(mayjuly) rename(mayjuly = educ2) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f1_educ3, keep(mayjuly) rename(mayjuly = educ3) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))
	(f1_educ4, keep(mayjuly) rename(mayjuly = educ4) color($col1) msymbol($sym1) ciopts(lcolor($col1)) offset(+0.05))

	(f0_age1, keep(mayjuly) rename(mayjuly = age1) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))
	(f0_age2, keep(mayjuly) rename(mayjuly = age2) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))
	(f0_age3, keep(mayjuly) rename(mayjuly = age3) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))
	(f0_age4, keep(mayjuly) rename(mayjuly = age4) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))
	(f0_age5, keep(mayjuly) rename(mayjuly = age5) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))

	(f0_wbho1, keep(mayjuly) rename(mayjuly = wbho1) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))
	(f0_wbho2, keep(mayjuly) rename(mayjuly = wbho2) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))
	(f0_wbho3, keep(mayjuly) rename(mayjuly = wbho3) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))
	(f0_wbho4, keep(mayjuly) rename(mayjuly = wbho4) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))

	(f0_educ1, keep(mayjuly) rename(mayjuly = educ1) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))
	(f0_educ2, keep(mayjuly) rename(mayjuly = educ2) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))
	(f0_educ3, keep(mayjuly) rename(mayjuly = educ3) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05))
	(f0_educ4, keep(mayjuly) rename(mayjuly = educ4) color($col2) msymbol($sym2) ciopts(lcolor($col2)) offset(-0.05)),

	order(age* . wbho* . educ*)
	rescale(100)
	title("")
	xtitle("May-July change in EPOP (p.p.)")
	xscale(range(0))
	xlabel(-2(1)2)
	xtick(-2(0.5)2)
	xline(0)
	xsize(6.75)
	headings(
		age1 = "{bf:Age}"
		wbho1 = "{bf:Race and ethnicity}"
		educ1 = "{bf:Educational attainment}",
		gap(0.5) offset(-.3))
	coeflabels(
		age1 = "25-29"
		age2 = "30-34"
		age3 = "35-39"
		age4 = "40-44"
		age5 = "45-49"
		wbho1 = "White non-Hispanic"
		wbho2 = "Black non-Hispanic"
		wbho3 = "Hispanic or Latinx"
		wbho4 = "Other non-Hispanic"
		educ1 = "Less than high school"
		educ2 = "High school graduate"
		educ3 = "Some college"
		educ4 = "College graduate"
	)
	grid(none)
	legend(order(1 2) label(1 "Women") label(2 "Men"));
#delimit cr

nicepdf "$basepath/output/heterogeneity.pdf", indirect replace

* Close the log file
unlaunch
